package cn.dgut.training.controller;


import cn.dgut.training.annotation.LoginUser;
import cn.dgut.training.dto.user.CollectDTO;
import cn.dgut.training.entity.user.Collect;
import cn.dgut.training.service.ICollectService;
import cn.dgut.training.vo.BaseVO;
import cn.dgut.training.vo.CommonResponse;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

@RestController
@RequestMapping("/collect")
public class CollectController {

    @Resource
    private ICollectService collectService;

    @PostMapping("/list")
    public CommonResponse list(@LoginUser Long userId, @RequestBody @Valid CollectDTO collectDTO){
        collectDTO.setUserId(userId);
        BaseVO baseVO=collectService.listCollect(collectDTO);
        return CommonResponse.success(baseVO);
    }

    @PostMapping("/addOrDelete")
    public CommonResponse addOrDelete(@LoginUser Long userId,@RequestBody @Valid CollectDTO collectDTO){
        collectDTO.setUserId(userId);
        collectService.addOrDelete(collectDTO);
        return CommonResponse.message();
    }

    /**
     *@Description 判断当前用户是否收藏该商品
     *@Param  [userId, goodsId]
     *@Return  java.lang.Integer
     *@创建人  刘祝蓝
     *@创建时间  2020/1/3 15:23
     */
    @GetMapping("/isCollect")
    public Integer isCollect(@RequestParam("userId") Long userId,@RequestParam("goodsId") Long goodsId){

        Collect collect=collectService.getOne(new LambdaQueryWrapper<Collect>().eq(Collect::getUserId,userId)
            .eq(Collect::getValueId,goodsId));

        if(collect==null){
            return 0;
        }else {
            return 1;
        }
    }
}

